package com.example.onlinemarket.service;

import com.example.onlinemarket.dao.GoodDao;
import com.example.onlinemarket.bean.Good;
import com.example.onlinemarket.bean.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.io.InputStream;
import java.util.List;
import java.util.stream.IntStream;

@Service
public class GoodService {
    private GoodDao goodDao;
    @Autowired
    public void setGoodDao(GoodDao goodDao){
        this.goodDao = goodDao;
    }

    public List<Good> getUpshelfGoodsByUserId(Integer userId){
        return goodDao.getUpshelfGoodsByUserId(userId);
    }

    public List<Good> getDownshelfGoodsByUserId(Integer userId){
        return goodDao.getDownshelfGoodsByUserId(userId);
    }

    public Good getGoodById(Integer id){
        return goodDao.getGoodById(id);
    }

    public boolean updateGoodInfo(Good good, InputStream is){
        return goodDao.updateGoodInfo(good, is);
    }

    public boolean updateGoodStatus(Integer goodId, boolean status) {
        return goodDao.updateGoodStatus(goodId, status);
    }

    public boolean delGoodById(Integer goodId) {
        System.out.print("鲜花存在,");
        boolean flag = goodDao.delGoodById(goodId);
        System.out.println(flag?"删除成功":"删除失败");
        return flag;
    }

    public boolean isGoodExist(Integer goodId) {
        return goodDao.isGoodExist(goodId);
    }

    public Integer getShopIdByGoodId(Integer goodId) {
        return getGoodById(goodId).getUserId();
    }

    //搜索鲜花
    public Model goodSearch(Model model, Integer currentPage, Integer pageSize, String searchContent, String species) {
        String msg = "不存在名为"+ searchContent +"的鲜花!";
        searchContent = "%"+ searchContent +"%";
        //判断是否按鲜花类型搜索
        if(species.equals("鲜花类型")) {
            species = "%";
        }
        //获取搜索记录条数
        Integer count = goodDao.checkForUpShelfGoodSearching(searchContent, species);
        if(count>0) {//若有记录
            int[] pageNum = null;
            Page page = new Page(currentPage, pageSize, count);
            //分页查询鲜花
            List<Good> upshelfGoods = searchForUpshelfGood(searchContent, species, page.getPageSize()*(page.getCurrentPage()-1), page.getPageSize());
            //生成合适的分页数组
            if (page.getTotalPages()>1) {
                pageNum = IntStream.range(1, page.getTotalPages() + 1).toArray();
            }else{
                pageNum = IntStream.range(1, 2).toArray();
            }
            model.addAttribute("upshelfGoods", upshelfGoods);
            model.addAttribute("pageNums", pageNum);
            model.addAttribute("pageSize",pageSize);
            model.addAttribute("totalRecords", count);
            model.addAttribute("totalPages", page.getTotalPages());
        }else{
            model.addAttribute("msg", msg);
            model.addAttribute("pageSize",0);
            model.addAttribute("totalRecords", 0);
            model.addAttribute("totalPages", 0);
        }
        return model;
    }

    public List<Good> searchForUpshelfGood(String searchContent, String species, Integer recorded, Integer pageSize) {
        return goodDao.searchForUpshelfGoodByPages(searchContent, species, recorded, pageSize);
    }


    public List<Good> searchForUpshelfGoodAll() {
        return goodDao.searchForUpshelfGoodByPagesAll();
    }



    public boolean addGood(Good good, InputStream is) {
        return goodDao.addGood(good, is) > 0;
    }

    public boolean updateGoodInfoWithoutImg(Good good) {
        return goodDao.updateGoodInfoWithoutImg(good);
    }

    public Model setErrorInfo(Model model, String goodName, String species, Double sellPrice, Double purchasePrice) {
        if (goodName.equals("")){
            String goodNameMsg = "鲜花名称不能为空！";
            model.addAttribute("goodNameMsg", goodNameMsg);
        }
        if(species.equals("鲜花类型")){
            String speciesMsg = "鲜花类型不能为空！";
            model.addAttribute("speciesMsg", speciesMsg);
        }
        if (sellPrice!=null){
            if (sellPrice <= 0){
                String sellPriceMsg = "鲜花售价非法(售价应为大于0的数字)";
                model.addAttribute("sellPriceMsg", sellPriceMsg);
            }
        }else{
            String sellPriceMsg = "鲜花售价不能为空！";
            model.addAttribute("sellPriceMsg", sellPriceMsg);
        }
        if (purchasePrice!=null) {
            if (purchasePrice <= 0) {
                String purchasePriceMsg = "鲜花进价非法(进价应为大于0的数字)";
                model.addAttribute("purchasePriceMsg", purchasePriceMsg);
            }
        }else{
            String purchasePriceMsg = "鲜花进价不能为空！";
            model.addAttribute("purchasePriceMsg", purchasePriceMsg);
        }
        return model;
    }

    public boolean isGoodNumLegal(Integer goodNum, Integer goodId) {
        Good good = getGoodById(goodId);
        return goodNum>0&&goodNum<=good.getQuantity();
    }

    public List<Integer> getShopAllGoodId(Integer userId) {
        return goodDao.getShopAllGoodId(userId);
    }

    public boolean updateSellNumAndQuantity(Integer orderId, boolean flag1) {
        List<Good> goods = goodDao.getGoodIdAndGoodNum(orderId);
        boolean flag = true;
        for(Good good: goods){
            if(flag1){
                flag &= goodDao.updateGoodSellNumAndQuantity(good.getGoodId(), good.getSellNum());
            }
            else{
                flag &= goodDao.updateGoodSellNumAndQuantity(good.getGoodId(),-1*good.getSellNum());
            }
        }
        return flag;
    }
}
